package org.liziyi.huffman.utils;

import org.liziyi.huffman.config.Config;

import java.awt.*;

/**
 * 绘图工具类
 *
 * @author liziyi
 */
public class PaintUtils {
    public static void drawCircle(Graphics2D g2d, int x, int y) {
        g2d.setColor(Color.BLACK);
        // 画圆的基准点是左上角
        g2d.fillOval(x - Config.treeNodeBaseSize / 2, y - Config.treeNodeBaseSize / 2, Config.treeNodeBaseSize, Config.treeNodeBaseSize);
    }

    public static void drawWord(Graphics2D g2d, String word, int x, int y) {
        g2d.setColor(Color.WHITE);
        g2d.setFont(new Font(Font.DIALOG, Font.BOLD, Config.treeWordBaseSize));
        // 画字的基准点是左下角
        g2d.drawString(word, x - Config.treeWordBaseSize / 2, y + Config.treeWordBaseSize / 2);
    }

    public static void drawLine(Graphics2D g2d, int x1, int y1, int x2, int y2) {
        g2d.setColor(Color.RED);
        g2d.drawLine(x1, y1, x2, y2);
        g2d.setColor(Color.BLUE);
        g2d.setFont(new Font(Font.DIALOG, Font.BOLD, (int) (Config.treeWordBaseSize * 0.7)));
        // huffman编码，左0，右1
        g2d.drawString(x2 < x1 ? "0" : "1",
                (x1 + x2) / 2 - (int) (0.35 * Config.treeWordBaseSize),
                (y1 + y2) / 2 + (int) (0.35 * Config.treeWordBaseSize));
    }
}
